package com.tencent.ied.bk.handler.impl;

import com.tencent.ied.bk.common.LocalStore;
import com.tencent.ied.bk.constant.CommonConstant;
import com.tencent.ied.bk.entity.BaseAbstractInfo;
import com.tencent.ied.bk.entity.SearchAbstractInfo;
import com.tencent.ied.bk.handler.Handler;
import com.tencent.ied.bk.handler.HandlerObject;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chen_yu
 * @date 2021/3/11 5:25
 */
@Slf4j
public class DataReadyHandler implements Handler {

    @Override
    public void handle(HandlerObject handlerObject) {
        List<SearchAbstractInfo> list = new ArrayList<>();
        for (BaseAbstractInfo baseAbstractInfo : LocalStore.BASE_ABSTRACT_INFO_LIST) {
            //将搜索结果存入缓存 searchKeyWord-> SearchAbstractInfo[]
            SearchAbstractInfo[] abstractInfoArray = LocalStore.SEARCH_KEY_ABSTRACT_MAPPING
                    .computeIfAbsent(handlerObject.getStrKeyWord(), k -> new SearchAbstractInfo[CommonConstant.ABSTRACT_NUM_LIMIT]);
            if (abstractInfoArray[baseAbstractInfo.getId()] == null) {
                abstractInfoArray[baseAbstractInfo.getId()] = new SearchAbstractInfo(baseAbstractInfo);
            }
            list.add(abstractInfoArray[baseAbstractInfo.getId()]);
        }
        handlerObject.setSearchAbstractInfoList(list);
    }
}
